$(document).ready(function () {
    $("[name='content']").eq(0).show();
    //显示日期
    getNowDate();
    //显示昵称
    idname();
    //添加清单输入框
    $("#add-list").on("click",function(){
         iuput="<input type=\"text\" id=\"list\" class=\"input\" placeholder=\"请输入清单名，Enter结束\">";
         $("#list-content").html(iuput);
         $("#list-content").toggle();//隐藏弹出框
         document.onkeydown=function(event){
            var e = event || window.event || arguments.callee.caller.arguments[0];
         if(e && e.keyCode==13){
            username = GetQueryString("username");
            addlist(username);  
            return false;
        }
        }
    });

    $(".radio").on("click",function (){
        $(this).attr("checked", true);
        $(this).siblings().attr("checked",false);
    });
    $(".radio-item").on("click",function (){
        $(this).attr("checked", true);
        $(this).siblings().attr("checked",false);
    });

    //切换
    $(".mmenuBtn div").on("click",function(){
        $(this).addClass("color").siblings().removeClass("color");
        $("[name='content']").hide().eq($(this).index()).show();
    });
});
//获取清单名
function addlist(username){
        //username = GetQueryString("username");
        list =$("#list");
        var lists={"listname":list.val(),"username":username};
        var data=getworkData();
        data.push(lists);
        saveworkData(data);
        showworkData(username);
        list.val("");
    }
//获取事项名
function additem(listname){
        //username = GetQueryString("username");
        list =$("#item");
        var lists={"itemname":list.val(),"listname":listname};
        var data=getitemData();
        data.push(lists);
        saveitemData(data);
        showitemData(listname);
        list.val("");
    }
//获取内容
function addcontent(itemnames){
        //username = GetQueryString("username");
        list =$("#content");
        var lists={"content":list.val(),"itemname":itemnames};
        var data=getcontentData();
        data.push(lists);
        savecontentData(data);
        showcontentData(itemnames);
        list.val("");
    }
//获取过期时间
function adddate(itemnames){
        donetime=$("#overtime");
        var lists={"itemname":itemnames,"donetime":donetime.val()};
        var data=getdateData();
        data.push(lists);
        savedateData(data);
        showdateData(itemnames);
    }
//创建时间
function saveNowTime(itemnames){
        datetime=getNowFormatDate();
        var lists={"itemname":itemnames,"nowtime":datetime};
        var data=getdateData();
        data.push(lists);
        savecreatdateData(data);
}
//显示创建时间
function showcreattime(itemnames){
        var date=localStorage.getItem("CreatdateInformation");    //取出所有的日期
        var nowdate=JSON.parse(date);
        for(i=0;i<nowdate.length;i++){
            if(itemnames==nowdate[i].itemname){
                datetime=nowdate[i].nowtime;
            }              
        }
        $("#creattime").html("创建时间:  "+datetime);
}
//显示id
function idname(){
    username = GetQueryString("username");
    var information=localStorage.getItem("Informations");
        var informationData = JSON.parse(information);
        var strname = "";
        for (var i = 0; i < informationData.length; i++) {
        if(informationData[i].email==username){
                strname=informationData[i].id;
                $("#idname").html(strname);
                showworkData(username);
        }        
    }
    
}
function getworkData() {
    var data=localStorage.getItem("workInformations");
    if(data!=null){
        return JSON.parse(data);
    }
    else{
        return [];
    }
}
function getitemData() {
    var data=localStorage.getItem("itemInformation");
    if(data!=null){
        return JSON.parse(data);
    }
    else{
        return [];
    }
}
function getcontentData() {
    var data=localStorage.getItem("contentInformation");
    if(data!=null){
        return JSON.parse(data);
    }
    else{
        return [];
    }
}
function getdateData() {
    var data=localStorage.getItem("dateInformation");
    if(data!=null){
        return JSON.parse(data);
    }
    else{
        return [];
    }
}
function getdateData() {
    var data=localStorage.getItem("CreatdateInformation");
    if(data!=null){
        return JSON.parse(data);
    }
    else{
        return [];
    }
}
function saveworkData(data){
   localStorage.setItem("workInformations",JSON.stringify(data));
}
function saveitemData(data){
   localStorage.setItem("itemInformation",JSON.stringify(data));
}
function savecontentData(data){
    localStorage.setItem("contentInformation",JSON.stringify(data));
}
function savedateData(data){
    localStorage.setItem("dateInformation",JSON.stringify(data));
}
function savecreatdateData(data){
    localStorage.setItem("CreatdateInformation",JSON.stringify(data));
}
function showworkData(username){
    var ol=$("#todolist");
    var data=localStorage.getItem("workInformations");    //取出所有的清单
    var listname=JSON.parse(data);
    var listnames="";
    var numberOflist = 0;
    for (var i = 0; i < listname.length; i++) {
        if(username==listname[i].username){
        listnames+="<li><input name=\"radio\" class=\"lists\" type=\"radio\" onclick=\"changeaction(" + i + ")\"/>"+listname[i].listname+"<img src=ToDoList完成版\\删除.png id=\"clearbutton\" class=\"clearbutton\" style=\"cursor:pointer\" onclick=\"deleteUser("+i+")\"></li>";
        numberOflist++;
    }
    }
    ol.html(listnames);
    $("#numberOflist").html(numberOflist);
}
function showitemData(listnames){
    var ol=$("#todoitem");
    var data=localStorage.getItem("itemInformation");    //取出所有的事项
    var itemname=JSON.parse(data);
    var Itemname="";
if(itemname.length==0){
    tip=$("#tip");
    tip.html("该清单无事项，请添加！");
    ol.html(Itemname);
    }
    else{
        for (var i = 0; i < itemname.length; i++) {
            if(listnames==itemname[i].listname){
            Itemname+="<li><input name=\"radio-item\" class=\"lists\" type=\"radio\" onclick=\"changeaction_item(" + i + ")\"/>"+itemname[i].itemname+"<img src=ToDoList完成版\\删除.png id=\"clearbutton\" class=\"clearbutton\" style=\"cursor:pointer\" onclick=\"deleteUser_item("+i+")\"></li>";
        }
    }   
         $("#tip").empty();
         ol.html(Itemname);
    }
}
function showcontentData(itemname){
    var div=$("#content_word");
    var data=localStorage.getItem("contentInformation");    //取出所有的事项内容
    var content=JSON.parse(data);
    var contents="";
    if(content.length==0){
        tip=$("#tip_content");
        tip.html("该事项无内容，请添加！");
        div.html(contents);
    }
    else{
        for (var i = 0; i < content.length; i++) {
            if(itemname==content[i].itemname){
            contents+="<div class=\"contents_word\">" + content[i].content +"<img src=ToDoList完成版\\垃圾桶.png id=\"clearbutton\" class=\"clearbutton_content\" style=\"cursor:pointer\" onclick=\"deleteUser_content("+i+")\"></div>";
        }
    }   
        $("#tip_content").empty();
        div.html(contents);
    }
}
function showdateData(itemname){

}
//获取地址栏参数
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}
//删除清单
function deleteUser(index){
    username = GetQueryString("username");
    var data=getworkData();
    data.splice(index,1);
    saveworkData(data);
    showworkData(username);
}
//删除事项
//参数：事项的index
function deleteUser_item(index){
    var data=localStorage.getItem("itemInformation");    //取出所有的事项
    var listname=JSON.parse(data);
    var listnames=listname[index].listname;
   // var Data=getitemData();
    listname.splice(index,1);
    saveitemData(listname);
    showitemData(listnames);
    return false;
}
//删除内容
function deleteUser_content(index){
    var data=localStorage.getItem("contentInformation");    //取出所有的事项
    var content=JSON.parse(data);
    var itemname=content[index].itemname;
   // var Data=getitemData();
    content.splice(index,1);
    savecontentData(content);
    showcontentData(itemname);
    return false;
}
//选中清单函数 => 得到事项
function changeaction(index) {
    //选中一个清单 => 知道清单名称 => 根据清单名称找到对应的事项 => 判断是否具有事项显示相应的内容
    var data=localStorage.getItem("workInformations");    //取出所有的清单
    var listname=JSON.parse(data);
    var listnames=listname[index].listname;    //清单名
    //清单-事项
    var dataitem=localStorage.getItem("itemInformation");
    var itemname=JSON.parse(dataitem);
    if(itemname.length == 0){
        tip=$("#tip");
        tip.html("该清单无事项，请添加！");
    }else{
         for(i = 0;i < itemname.length; i++){
             if(listnames!=itemname[i].listname){  
                 tip=$("#tip");
                 tip.html("该清单无事项，请添加！");
                //return ;
             }
             else{                 
                 showitemData(listnames);
             }
    }
    }  
    // 添加事项的操作： 不论是否具有事项，都会具有的操作
    $("#add-item").on("click",function(){
         iuput="<input type=\"text\" id=\"item\" class=\"input\" placeholder=\"请输入事项名，Enter结束\">";
         $("#item-content").html(iuput);
         $("#item-content").toggle();//隐藏弹出框
         document.onkeydown=function(event){
            var e = event || window.event || arguments.callee.caller.arguments[0];
         if(e && e.keyCode==13){
            additem(listnames);  
            return false;
        }
        }
    });
    
}
//选中事项,添加内容
function changeaction_item(index){
    var data=localStorage.getItem("itemInformation");    //取出所有的事项
    var itemname=JSON.parse(data);
    var itemnames=itemname[index].itemname;
    var datacontent=localStorage.getItem("contentInformation");
    var content=JSON.parse(datacontent);
    if(content== null){
        tip=$("#tip_content");
        tip.html("该事项无内容，请添加！");
    }
    else{
         for(i = 0;i < content.length; i++){
             if(itemnames!=content[i].itemname){  
                 tip=$("#tip_content");
                 tip.html("该事项无内容，请添加！");
             }
             else{                 
                 showcontentData(itemnames);;
             }
    }
    }
    $("#add-content").on("click",function(){
         iuput="<input type=\"text\" id=\"content\" class=\"input\" placeholder=\"输入内容，Enter结束\">";
         $("#content_input").html(iuput);
         $("#content_input").toggle();//隐藏弹出框
         document.onkeydown=function(event){
            var e = event || window.event || arguments.callee.caller.arguments[0];
         if(e && e.keyCode==13){
            addcontent(itemnames);  
            return false;
        }
        }
    });
        showcreattime(itemnames);
        donetime=$("#donetime");
        donetime.html("过期时间: <input type=\"text\" id=\"overtime\" class=\"overtime\">");

}


//获取当前时间精确到分
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes();
    return currentdate;
}
//获取当前日期
function getNowDate() {
    var date = new Date();
    var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
    datetime=$("#NowDate");
    datetime.html(currentdate);
}